// 弹层
// $('#mask').show();
// 提现tab切换
$(function(){
	var lis=$('.l_title>ul>li');
    var aobj = $('.l_title ul>li>a');
    var main_li=$('.l_cont>div');
    for(var i=0; i<lis.length; i++){
	        lis[i].setAttribute('index',i);
	        lis[i].onclick=function(){
	            for(var j=0; j<lis.length; j++) {
	                lis[j].removeAttribute('class');
	            }
	            this.setAttribute('class','current');

	            for(var k=0; k<main_li.length; k++) {
	                main_li[k].style.display='none';
	            }
	            this.style.display='block';
	            main_li[this.getAttribute('index')].style.display='block';
	    }
	}
})
//上传银行卡
$(function (e) {
    $("#uploadForm").on('submit', function(e){
        e.preventDefault();
        $.ajax({
            url: imgurl,
            type: "POST",
            data:  new FormData(this),
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            contentType: false,
            cache: false,
            processData: false,
            // 显示加载图片
            beforeSend: function () {
                $('.loading-shadow').addClass('active');
            },
            success: function (data) {
                $('#bank_img').attr('src',data.msg);
                $('#bankimg').attr('value',data.msg);
            },
            error: function(){}
        });
    });
});
$(function(){
    var frontBox = document.querySelector("#frontBox");
    var front_icon = document.querySelector("#bankfile");
    frontBox.onclick = function(){
        front_icon.click();     //file1触发事件
    }
    var myFile;
    front_icon.onchange = function(){     //当事件改变的时候触发
        // 获取上传文件的大小
        var file_size=parseInt((document.getElementById('bankfile').files[0].size)/1024/1024)
        if(file_size <= 2) {
            $("#uploadForm").trigger('submit');
        }else{
            layer.tips('图片大小不能超过2M!','#img1',{time:1000});
        }
    }
})

// 上传银行卡
function uploadBank(obj){
	// 银行卡
	var banknum   = $.trim($('input[name=cardbank]').val());
	// 支行名
	var zhihang   = $.trim($('input[name=zhihang]').val());
	// 银行卡照片
	var bankimg = $('#bankimg').attr('value');

    var bank_province   = $('#province').val();
    var bank_city   = $('#city').val();
    var bank = $('#bank_hang').val();

    var isChinaName=/^[\u4E00-\uFA29]|[\uE7C7-\uE7F3]$/;
    var isBankCard =isBankCard(banknum);

	// 判断是否填写
	if(banknum == ""){
		layer.tips('请输入银行卡号!','#banknum',{time:1000});
        return;
	}else if(!isBankCard){
        layer.tips('请输入正确的银行卡号!','#banknum',{time:1000});
        return;
    }else if(zhihang == ""){
		layer.tips('请输入开户行信息!','#zhihang',{time:1000});
        return;
	}else if(!isChinaName.test(zhihang)){
        layer.tips('请输入正确的支行!','#zhihang',{time:1000});
        return;
    }else if(bankimg == ""){
		layer.tips('请上传银行卡照片!','#bank_img',{time:1000});
        return;
	}else if(bank == "选择银行") {
        layer.tips('请选择银行!','#bank_hang',{time:1000});
        return;
    }
    $(obj).removeAttr('onclick');
	$.ajax({
        url:dobank,
        type: 'post',
        data: {'bankimg':bankimg,'bank_number':banknum,'bank_branch':zhihang,'bank':bank,'bank_province':bank_province,'bank_city':bank_city},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        dataType:'json',
        success: function(res){
            if(res.status == 1){
               //未登录
               showCeng();
                $('#tanCeng>a>button').hide();
                $('#tanCeng').css({'width':'250px','height':'80px','line-height':'80px'}).html("请先<a href='"+gotologin+"' style='cursor:pointer;color:#3b9ce0;font-size: 20px;'>登录!</a>");
            }if(res.status == 3){
                //银行卡数已达上限
                layer.msg(res.msg,{time:1000});
                $(obj).attr('onclick','uploadBank(this)');
                return;
            }else if(ziliaotype == 1){
                //未完善资料
                showCeng();
                $('#tanCeng>a>button').hide();
                $("#tanCeng").html("您未完善基本信息和认证信息，<a href='"+gowanshan+"' style='cursor:pointer;color:#3b9ce0;'>请完善!</a>");
            }else if(res.status == 2){
                    $('#mask').show();
                    $('.scc_pic').hide();
                    $('.scc_txt span').html('提交失败，重新输入！');
               //提交失败
               setInterval(function(){
                    $('#mask').hide();
                    $('.scc_pic').hide();
                    $('.scc_txt span').html('提交失败，重新输入！');
               },2000)
                $(obj).attr('onclick','uploadBank(this)');
            }else{
               //提交成功
                    $('#mask').show();
                    $('.l_bank_success').show();
               setInterval(function(){
                    $('#mask').hide();
                    $('.l_bank_success').hide();
               },2000);
                window.location.reload();
            }
        }        
    });
    // 银行卡号校验
//bankno为银行卡号 banknoInfo为显示提示信息的DIV或其他控件
    function isBankCard(bankno){
        var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）

        var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
        var newArr=new Array();
        for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
            newArr.push(first15Num.substr(i,1));
        }
        var arrJiShu=new Array();  //奇数位*2的积 <9
        var arrJiShu2=new Array(); //奇数位*2的积 >9

        var arrOuShu=new Array();  //偶数位数组
        for(var j=0;j<newArr.length;j++){
            if((j+1)%2==1){//奇数位
                if(parseInt(newArr[j])*2<9)
                    arrJiShu.push(parseInt(newArr[j])*2);
                else
                    arrJiShu2.push(parseInt(newArr[j])*2);
            }
            else //偶数位
                arrOuShu.push(newArr[j]);
        }

        var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
        var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
        for(var h=0;h<arrJiShu2.length;h++){
            jishu_child1.push(parseInt(arrJiShu2[h])%10);
            jishu_child2.push(parseInt(arrJiShu2[h])/10);
        }

        var sumJiShu=0; //奇数位*2 < 9 的数组之和
        var sumOuShu=0; //偶数位数组之和
        var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
        var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
        var sumTotal=0;
        for(var m=0;m<arrJiShu.length;m++){
            sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
        }

        for(var n=0;n<arrOuShu.length;n++){
            sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
        }

        for(var p=0;p<jishu_child1.length;p++){
            sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
            sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
        }
        //计算总和
        sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);

        //计算Luhm值
        var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
        var luhm= 10-k;

        if(lastNum==luhm){
            //$("#banknoInfo").html("Luhm验证通过");
            return true;
        }
        else{
            //$("#banknoInfo").html("银行卡号必须符合Luhm校验");
            return false;
        }
    }
}
// 银行卡号校验
//bankno为银行卡号 banknoInfo为显示提示信息的DIV或其他控件
function isBankCard(bankno){
    var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）

    var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
    var newArr=new Array();
    for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
        newArr.push(first15Num.substr(i,1));
    }
    var arrJiShu=new Array();  //奇数位*2的积 <9
    var arrJiShu2=new Array(); //奇数位*2的积 >9

    var arrOuShu=new Array();  //偶数位数组
    for(var j=0;j<newArr.length;j++){
        if((j+1)%2==1){//奇数位
            if(parseInt(newArr[j])*2<9)
                arrJiShu.push(parseInt(newArr[j])*2);
            else
                arrJiShu2.push(parseInt(newArr[j])*2);
        }
        else //偶数位
            arrOuShu.push(newArr[j]);
    }

    var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
    var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
    for(var h=0;h<arrJiShu2.length;h++){
        jishu_child1.push(parseInt(arrJiShu2[h])%10);
        jishu_child2.push(parseInt(arrJiShu2[h])/10);
    }

    var sumJiShu=0; //奇数位*2 < 9 的数组之和
    var sumOuShu=0; //偶数位数组之和
    var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
    var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
    var sumTotal=0;
    for(var m=0;m<arrJiShu.length;m++){
        sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
    }

    for(var n=0;n<arrOuShu.length;n++){
        sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
    }

    for(var p=0;p<jishu_child1.length;p++){
        sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
        sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
    }
    //计算总和
    sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);

    //计算Luhm值
    var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
    var luhm= 10-k;

    if(lastNum==luhm){
        //$("#banknoInfo").html("Luhm验证通过");
        return true;
    }
    else{
        //$("#banknoInfo").html("银行卡号必须符合Luhm校验");
        return false;
    }
}
$(function(){
    if(ziliaotype == 1){
        //未完善资料
        showCeng();
        $('#tanCeng>a>button').hide();
        $("#tanCeng").html("您未完善基本信息和认证信息，<a href='"+gowanshan+"' style='cursor:pointer;color:#3b9ce0;'>请完善!</a>");
    }
})

// 收益内容收起
$('.shouqi').click(function(){
    var self = $(this);
	$(this).parent().siblings('.shoucon').slideToggle(function(){
        if ($(this).is(':hidden')) {
            self.html("展开<a href='javascript:;'><img src='../../images/toright.png' style='margin-left:8px;'></a>");
        }else{
            self.html("收起<a href='javascript:;'><img src='../../images/shou.png' style='margin-left:8px;'></a>");
        }
    });
})


// 申请提现--选择银行卡
function change(){
	$('.changecon').slideToggle();
}

// 申请提现--删除银行卡
/*$(function(){
    var del=$('.changecon .change');
    del.bind('click',function(){
        $(this).parent().remove();
    })
})*/
// 提现小圆点样式
$(function(){
    $('.changecon i').bind('click',function(){
        $(this).addClass('current').parent().siblings().children('i').removeClass('current');
        // 当前div中的内容展示在上面
        // 获取当前点击的银行卡名称和卡号
        var bankname=$(this).siblings('.bank_name').html();
        var cardnum=$(this).siblings('.card_num').html();
        var bankid=$(this).find('input').val();

        $('#bank_name').html(bankname);
        $('#card_num').html(cardnum);
        $('#bank_name').attr('bank_id',bankid);
        $('.changecon').slideToggle();

        $.ajax({
            url:doselectbank,
            type: 'post',
            data: {'bankid':bankid},
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            dataType:'json',
            success: function(res){

            }
        });
    })
})

// 订阅领取按钮
function orderdraw(months,money,obj){
    var myDate = new Date();
    var year   = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
    var month  = myDate.getMonth(); //获取当前月份(0-11,0代表1月)
    month = (month+1)<10 ? "0"+(month+1):month;
    if(months == (year+'年'+(month)+'月')){
        $('#l_maskDiv').show();
        setInterval(function(){
            $('#l_maskDiv').hide();
        },2000)
    }else if(months > (year+'年'+(month)+'月')){
        $('#l_maskDiv').show();
        $('#l_maskDiv').html('领取月份不正确');
        setInterval(function(){
            $('#l_maskDiv').hide();
        },2000)
    }else{
        $(obj).removeAttr('onclick');
        var bankid = $('#bank_name').attr('bank_id');
        $.ajax({
            url:dohandandraw,
            type: 'post',
            data:{'months':months,'money':money,'bankid':bankid},
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            dataType:'json',
            success: function(res){
                if(res.status == 3){
                    layer.msg(res.msg,{time:1000});
                    $('.l_title>ul li:eq(1)').click();
                    $(obj).attr('onclick',"orderdraw('"+months+"',"+money+",this)");
                }else{
                    layer.msg(res.msg);
                    window.location.reload();
                }
            }
        });
    }

}
// 周赛领取按钮
function weekdraw(rankingid,obj){
    $(obj).removeAttr('onclick');
    var bankid = $('#bank_name').attr('bank_id');
    $.ajax({
        url:doweekdraw,
        type: 'post',
        data:{'rankingid':rankingid,'bankid':bankid},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        dataType:'json',
        success: function(res){
            if(res.status == 3){
                layer.msg(res.msg,{time:1000});
                $('.l_title>ul li:eq(1)').click();
                $(obj).attr('onclick','weekdraw('+rankingid+',this)');
            }else{
                layer.msg(res.msg);
                window.location.reload();
            }
        }
    });
}

// 去添加银行卡页
$('.gotoBank').click(function(){
    $('.l_title>ul li:eq(1)').click();  
})

//删除银行卡
function delbank(bankid,obj,dijige){
    var num   = $('.changecon li').length;
    var ulobj = $(obj).parent().parent();
    $.ajax({
        url:dodelbank,
        type: 'post',
        data: {'bankid':bankid},
        headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
        dataType:'json',
        success: function(res){
            if(res.status == 0){ //删除成功
                layer.msg(res.msg);
                $(obj).parent().remove();
                if(num == 1){
                    $('.bank_box').html('<span style="padding-left:30px;">请点击下方按钮添加您的银行卡</span>');
                }
                if(dijige == 0){
                    var liobj = ulobj.find('li:eq(0)');
                    var bank_name_1 = liobj.find('.bank_name').html();
                    var card_num_1  = liobj.find('.card_num').html();
                    var bank_id_1   = liobj.find('.bankid').val();

                    $('.bank_box').find('#bank_name').html(bank_name_1);
                    $('.bank_box').find('#bank_name').attr('bank_id',bank_id_1);
                    $('.bank_box').find('#card_num').html(card_num_1);
                    liobj.find('i').attr('class','current');
                }
                $('.gotoBank').css('display','block');
            }if(res.status == 1) { //删除失败
                layer.msg(res.msg);
            }else{ //信息有误
                layer.msg(res.msg);
            }
        }
    });
}




